package com.kk.travel.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kk.travel.admin.User;
import com.kk.travel.admin.Word;
import com.kk.travel.service.UserService;

import comkk.travel.utils.DataFormatUtil;


public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();

		System.out.println(servletPath);

		String methodName = servletPath.substring(1);
		methodName = methodName.substring(0, methodName.length() - 3);
		Method method;
		try {
			method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private UserService userService=new UserService();
	private DataFormatUtil dateUtil=new DataFormatUtil();

	

	protected void keepWords(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      String keyword = request.getParameter("keyword");
	      if(keyword!=null && keyword!=""){
	    	  HttpSession session = request.getSession();
	    	  User user = (User) session.getAttribute("user");
	    	  if(user==null){
	    		  session.setAttribute("k", keyword);
	    		  System.out.println("成功");
	    	  }
	      }
	}
		
	protected void count(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   
		String strId=request.getParameter("id");
		String strCount=request.getParameter("count");
		System.out.println(strId+"---"+strCount);
		if(strId!=null && strCount!=null && strId!="" && strCount!=""){
			Integer id=Integer.parseInt(strId.trim());
			Integer count=Integer.parseInt(strCount.trim());	
			userService.updateWordCount(id,count);
			
    		HttpSession session=request.getSession();
    		
    		
    		List<Word> words=userService.getWords();
    		
    		session.setAttribute("words", words);
		}
		
	}
		protected void toSpeak(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   String email=request.getParameter("email");
	   String word=request.getParameter("words");


	   System.out.println(email+word);
	   if(email!=null && email!="" && word!=null && word!=""){

		   userService.speak(email,word,dateUtil.formatDate(new Date()));
		   
   		HttpSession session=request.getSession();
   		
   		List<Word> words=userService.getWords();
   		
   		session.setAttribute("words", words);
   		session.removeAttribute("k");
	   }

	}	
	protected void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("flag", "1");
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	protected void validateName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("username");
		String mail=request.getParameter("useremail");
		System.out.println(name+"===="+mail);

	        ServletOutputStream os=response.getOutputStream();
	        os.write(userService.result(name, mail).getBytes());

	}
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		String paramCode=request.getParameter("code");
		
		String code=(String) request.getSession().getAttribute("CHECK_CODE_KEY");
		
		System.out.println(username+email+password+paramCode+"--"+code);
		
		ServletOutputStream os=response.getOutputStream();
		 if(username!=null && password!=null && username!="" && password!="" && email!=null && email!="" && paramCode!=null && paramCode!=""){	 
			 if(code.equals(paramCode.trim().toLowerCase())){				 
				 User user=new User(username, email, password);		
				 userService.InsertUser(user);		
				 
				 request.getSession().setAttribute("user", user);
				 os.write("success".getBytes());
				 return;
			 }
		 }		
		os.write("error".getBytes());
	}
	
	protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String page=request.getParameter("page");
	    
	    request.getSession().removeAttribute("k");
	    request.getSession().removeAttribute("user");
	    
	    response.sendRedirect(page+".jsp");
	}
		
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        ServletOutputStream out = response.getOutputStream();
        	System.out.println(username+password);
        	if(username!=null && password!=null && username!="" && password!=""){
        		User user=new User(username, username, password);
        		
        		if(userService.validateUser(user)!=null){
        			
        			User user1=userService.validateUser(user);
        			HttpSession session=request.getSession();
        			session.setAttribute("user", user1);   
        			out.write("success".getBytes());
        			System.out.println("1");
        			return;
        		}
        		
        	}
        	
        	out.write("error".getBytes());
        }
        
 
	
	
	


}
